package at.fhooe.stefanirndorfer.sound;

import at.fhooe.stefanirndorfer.effects.Effect;
import at.fhooe.stefanirndorfer.effects.EffectUtils;
import at.fhooe.stefanirndorfer.effects.Filter;
import at.fhooe.stefanirndorfer.effects.LowPassFilter;
import at.fhooe.stefanirndorfer.logging.Log;


/**
 * plays a Sound with a certain deley in milliseconds and effects
 * @author stefanirndorfer
 *
 */
public class SoundPlayerEFX extends SoundPlayer{
	
	private static final int NUM_EFX_SLOTS = 1;
	private static final int NUM_EFX = 1;
	
	/** Delay in milliseconds */
	int delay;
	private int[] auxEffectSlots;
	private int[] effects;
	
	
	

	public SoundPlayerEFX(SoundManager sm, String[] soundIds, int delay, double posX,
			double posY, int windowWidth, int windowHeight) {
		
		super(sm, soundIds, posX, posY, windowHeight, windowHeight);
		
		this.delay = delay;
		auxEffectSlots = EffectUtils.createEffectSlots(NUM_EFX_SLOTS);
		effects = EffectUtils.createEffects(NUM_EFX);	
	}

	
	
	
	public void  playDelayed(String id, boolean looping){
		new PlayDelayedThread(this, id, looping, null, null).start();
	}
	
	public void  playDelayed(String id, boolean looping, Filter filter){
		new PlayDelayedThread(this, id, looping, filter, null).start();
	}
	
	public void  playDelayed(String id, boolean looping, Effect effect){
		new PlayDelayedThread(this, id, looping, null, effect).start();
	}
	
	public void  playDelayed(String id, boolean looping, Filter filter, Effect effect){
		new PlayDelayedThread(this, id, looping,filter,effect).start();
	}
	
	public void  playDelayed(String id){
		playDelayed(id, false);
	}
	
	public void playDelayed(String sId, Filter filter) {
		playDelayed(sId, false, filter);
	}

	
	///////////////////////////////////////////////////
	/// Overridden Methods
	///////////////////////////////////////////////////
	
	@Override
	public void preloadSounds() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause(String id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stop(String id) {
		// TODO Auto-generated method stub
		
	}

	
	
	
	///////////////////////////////////////////////////
	/// Getters and setters
	///////////////////////////////////////////////////

	public long getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}
	
	
	/**
	 * 
	 * @author stefanirndorfer
	 *
	 */
	public class PlayDelayedThread extends Thread {
		
		SoundPlayerEFX spe;
		String soundId;
		boolean looping;
		SoundManager sm;
		OpenALSource source;
		Filter filter;
		Effect effect;
		
		public PlayDelayedThread(SoundPlayerEFX spe, String soundId, boolean looping, Filter filter, Effect effect){
			this.spe = spe;
			this.soundId = soundId;
			this.looping  = looping;
			this.filter = filter;
			this.effect = effect;
			sm = spe.sm;
		}
		
		public PlayDelayedThread(SoundPlayerEFX spe, String soundId, boolean looping, Effect effect){
			this.spe = spe;
			this.soundId = soundId;
			this.looping  = looping;
			this.filter = null;
			this.effect = effect;
			sm = spe.sm;
		}
		
		public PlayDelayedThread(SoundPlayerEFX spe, String soundId, boolean looping, Filter filter){
			this.spe = spe;
			this.soundId = soundId;
			this.looping  = looping;
			this.filter = filter;
			this.effect = null;
			sm = spe.sm;
		}
		
		public void run(){
			try {
				Thread.sleep(spe.delay);
				spe.source.play(sm.getOpenALWave(soundId), looping, sourcePos, sourceVel, spe, filter, effect);				
				Log.info(this, "Delayed Sound played");
				
			} catch (InterruptedException e) {
				Log.info(this, "Error playing reflection");
				e.printStackTrace();
			}
		}
	}


	

	
	
}
